package com.eveinfo.sql;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class Unzip {
	public static void unzip(Context context, Handler mHandler, Thread thread) {
		String zipname = "dominion.zip";
		try {
			File file = new File("/sdcard/", zipname);
			FileInputStream fis = new FileInputStream(file);
			ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
			ZipEntry entry;

			while ((entry = zis.getNextEntry()) != null) {
				System.out.println("Unzipping: " + entry.getName());

				int size;
				byte[] buffer = new byte[512* 1048];

				File fileOutput = new File("/sdcard/", entry.getName());
				FileOutputStream fos = new FileOutputStream(fileOutput);
				BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
				int total = 0;
				while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
					total += size;
					Message msg = mHandler.obtainMessage();
					Bundle b = new Bundle();
					b.putInt("total", total);
					msg.setData(b);
					mHandler.sendMessage(msg);
					bos.write(buffer, 0, size);
				}
				bos.flush();
				bos.close();
			}

			zis.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}